package cz.semjob.linguistic.processor;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.management.ManagementFactory;

import org.apache.log4j.Logger;

import cz.semjob.linguistic.util.Util;

public class TreexProcessor implements ILinguisticProcessor {
	
	
	private static String treexLocation = "/host/diploma/workspace/linguistic-processing/src/test/resources/treex.sh";
	private static Logger logger = Logger.getLogger(TreexProcessor.class);

	public void processInputFile(File inputFile, File outputFile)
			throws LinguisticProcessingException {
		
		if (!inputFile.exists() ) {
			throw new LinguisticProcessingException("Input file does not exist");
		}
		logger.debug("Processing file: " + inputFile.getAbsolutePath());
		logger.debug("Output file: " + outputFile.getAbsolutePath());
		
		
		// cat /home/ivivivi/diploma/processed/jd_0017.txt  |  treex -Lcs  Read::Text W2A::CS::Segment use_lines=1 W2A::CS::Tokenize W2A::CS::TagMorce W2A::CS::ParseMSTAdapted Write::CoNLLX to=~/diploma/processed/jd_0017.connlx
		String command = treexLocation + " " + inputFile + " " + outputFile ;
		try {
			Util.executeCommand(command);
		} catch (IOException e) {
			throw new LinguisticProcessingException(e);
		}
	
		
	}
	
	
	
	private String createTemporaryFile(final String textToFile) throws IOException {
		String path = "";
		try {
			path = "/tmp/" + ManagementFactory.getRuntimeMXBean().getName() + ".txt";
			FileOutputStream out = new FileOutputStream(path);
			PrintStream ps = new PrintStream(out, true, "UTF-8");
			ps.print(textToFile);
			out.close();

		} catch (IOException ex) {
			logger.error("Temporary file couldnt be created: ", ex);
			throw ex;
		}
		return path;

	}

}
